package com.fastfly.spring.mvc.controller;

import java.security.Principal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

import com.fastfly.domain.City;
import com.fastfly.domain.Flight;
import com.fastfly.spring.mvc.editor.BaseEntityEditor;

@Controller 
public class FlightController extends BaseController {


	@SuppressWarnings("unchecked")
    @InitBinder
    public void initBinder(WebDataBinder dataBinder, WebRequest request) {
    	dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));    	
    	
    	List<City> list = (List<City>) request.getAttribute("cityList", WebRequest.SCOPE_REQUEST);
        dataBinder.registerCustomEditor(City.class, new BaseEntityEditor( list ) );
    }
    
    
	@SuppressWarnings("unchecked")
	@ModelAttribute("cityList")
	protected List<City> setupCityList(WebRequest request) {
		List<City> list = flightService.findAll(City.class);		
		request.setAttribute("cityList", list, WebRequest.SCOPE_REQUEST);
		return list;
	}
    

	@RequestMapping(method = RequestMethod.GET)
	public void index(Principal currentUser, Model model, WebRequest request) {
		log.trace("index currentUser: " + currentUser);		
		model.addAttribute(new Flight());
	}
	
	
	
	@RequestMapping(method = RequestMethod.POST)
	public String searchFlight(@ModelAttribute Flight flightQuery, BindingResult result, Model model, WebRequest request) {
		flightQuery.validateSearchFlight(result);
		if (! result.hasErrors()) {
			List<Flight> list = flightService.findFlightsByCity(flightQuery);
			request.setAttribute("flightList", list, WebRequest.SCOPE_REQUEST);				
		}
		return "flight/index";
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public void status(Model model) {
		log.trace("status");		
		model.addAttribute(new Flight());		
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String searchFlightStatus(@ModelAttribute Flight flightQuery, BindingResult result, Model model, WebRequest request) {
		flightQuery.validateDepartureDate(result);
		if ( Flight.SEARCH_CITY.equalsIgnoreCase(flightQuery.getSearchBy()) ) {
			flightQuery.validateCities(result);
		}		
		if (! result.hasErrors()) {
			List<Flight> list = null;
			if ( Flight.SEARCH_BY_FLIGHT_NUMBER.equalsIgnoreCase(flightQuery.getSearchBy())  ) {
				list = flightService.findFlightsByNumber(flightQuery);
			} else {
				
				list = flightService.findFlightsByCity(flightQuery);
			}		
			request.setAttribute("flightList", list, WebRequest.SCOPE_REQUEST);
			request.setAttribute("showStatus", true, WebRequest.SCOPE_REQUEST);
			//model.addAttribute(flightQuery);			
		}
		return "flight/status";
	}
	

}
